package ui.face
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	import globals.Global;

	import ui.component.Button;
	import ui.component.Label;

	import util.DisplayUtil;
	import util.UIUtil;

	public class ActionIcon extends Sprite
	{
		public var opened:Boolean;
		/////////////////
		private var _id:int;
		private var _items:Array;
		private var _iconBg:Bitmap;
		private var _itemBg:Bitmap;
		private var overLayer:Sprite;
		private var _handler:Function;

		public function ActionIcon(iconUrl:String, labelName:String = '', handler:Function = null)
		{
			overLayer = new Sprite();
			opened = false;
			buttonMode = true;
			_handler = handler;

			_iconBg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("sc_icon_bg"));
			var bitmap:Bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData(iconUrl));
			bitmap.x = (_iconBg.width - bitmap.width) / 2;
			bitmap.y = (_iconBg.height - bitmap.height) / 2;
			this.addChild(bitmap);
			var label:Label = new Label(12, 0xFFFFFF, false, "left", true, 0x000000);
			label.text = labelName;
			label.x = (_iconBg.width - label.width) * 0.5;
			label.y = _iconBg.height - label.height;
			this.addChild(label);

			_itemBg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("sc_bg"));
			_itemBg.x = 0;
			_itemBg.y = -154;
			overLayer.addChild(_itemBg);
			addEventListener(MouseEvent.CLICK, toggleHandler);
			addEventListener(MouseEvent.ROLL_OVER, overHandler);
			addEventListener(MouseEvent.ROLL_OUT, outHandler);

			_items = new Array();
		}

		public function addItem(iconUrl:String, labelText:String, handler:Function):void
		{
			var item:ActionItem = new ActionItem(iconUrl, labelText, handler);
			item.x = 0;
			item.y = -66 * (_items.length + 1) - 14;
			overLayer.addChild(item);
			_items.push(item);
		}

		public function layout(open:Boolean):void
		{
			opened = open;

			if(open)
			{
				this.addChildAt(overLayer, 0);
				this.addChildAt(_iconBg, 0);
			}
			else
			{
				DisplayUtil.removeChild(this, overLayer);
				DisplayUtil.removeChild(this, _iconBg);
			}
		}

		private function toggleHandler(event:MouseEvent):void
		{
			if(_handler != null)
			{
				_handler(this);
			}
		}

		private function overHandler(event:MouseEvent):void
		{
			this.addChildAt(_iconBg, 0);
		}

		private function outHandler(event:MouseEvent):void
		{
			if(!opened)
			{
				DisplayUtil.removeChild(this, _iconBg);
			}
		}

		public function get id():int
		{
			return _id;
		}

		public function set id(value:int):void
		{
			_id = value;
		}
	}
}

import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextFieldAutoSize;

import globals.Global;

import ui.component.Button;
import ui.component.Label;

class ActionItem extends Sprite
{
	private var _handler:Function;

	public function ActionItem(iconUrl:String, labelText:String, handler:Function)
	{
		var bg:Bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData(iconUrl));
		bg.x = 28 - bg.width * 0.5;
		addChild(bg);

		var label:Label = new Label(12, 0xFFFFFF);
		label.x = 16;
		label.y = 36;
		label.text = labelText;
		label.height = 20;
		addChild(label);

		_handler = handler;

		buttonMode = true;
		addEventListener(MouseEvent.CLICK, clickHandler);
	}

	private function clickHandler(event:MouseEvent):void
	{
		if(_handler != null)
		{
			_handler();
		}
	}
}